<?php

/**
 * main actions.
 *
 * @package    sf_sandbox
 * @subpackage main
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class mainComponents extends sfActions {

    public function executeMenu() {
        $obj = MenuTable::getInstance()->getByName($this->menu_name);
        $this->menu_items = $obj->getMenuItems($this->getUser()->getCulture());
    }

    public function executeFooter_menu() {
        $obj = MenuTable::getInstance()->getByName('footer_menu');
        $this->culture = $this->getUser()->getCulture();
        $this->menu_items = $obj->getMenuItems($this->culture, $this->column, 5);
    }

    public function executePhoto_video() {

        $homepage_contents = Doctrine_Query::create()
                ->select('c.id, c.slug, t.title as title, t.brief as brief, c.image as image')
                ->from('Content c')
                ->leftJoin('c.Translation t')
//                ->innerJoin('c.Image i')
                ->where('on_homepage = 1')
                ->andWhere('t.title <> ""')
                ->andWhere('t.lang = ?', $this->getUser()->getCulture())
                ->orderBy('c.weight DESC')
                ->limit(10)
                ->fetchArray();

        $json = array();
        foreach ($homepage_contents as $one) {
            $json['title'][] = truncate_text($one['title'], 80, '...', true);
            $json['brief'][] = $one['brief'];
            $json['image'][] = get_resized_image(0, 366, get_local_image_path($one['image']), 'height'); //563
            $json['slug'][] = $one['slug'];
        }
        $this->json = json_encode($json);
        $this->homepage_contents = $homepage_contents;

        // ------------------------
        $this->yt_videos = Doctrine_Query::create()
                ->from('HomeVideo')
                ->orderBy('weight DESC')
                ->limit(10)
                ->where('culture = ?', $this->getUser()->getCulture())
                ->fetchArray();
    }

    public function executeRelated_news() {
        $q = Doctrine_Query::create()
                ->select('c.id, t.title as title, t.brief as brief, c.slug, c.category_id')
                ->from('Content c')
                ->leftJoin('c.Translation t')
                ->where('category_id = ?', $this->category_id)
                ->addWhere('t.lang = ?', $this->getUser()->getCulture())
                ->addWhere('c.id <> ?', $this->exclude)
                ->addWhere('c.is_player_page = 0')
                ->addWhere('t.title <> ""')
                ->orderBy('weight DESC')
                ->limit(3);
        $this->items = $q->fetchArray();
        $this->count = count($this->items);
    }

    public function executeRelated_news_blog() {
        $q = Doctrine_Query::create()
                ->select('c.id, t.title as title, t.info as info, c.slug')
                ->from('Blog c')
                ->leftJoin('c.Translation t')
//                ->where('category_id = ?', $this->category_id)
                ->addWhere('t.lang = ?', $this->getUser()->getCulture())
                ->addWhere('c.id <> ?', $this->exclude)
                ->orderBy('c.updated_at DESC')
                ->addWhere('t.title <> ""')
                ->limit(3);
        $this->items = $q->fetchArray();
        $this->count = count($this->items);
    }

    public function executeVideo_alghad() {
        $this->items = Doctrine_Query::create()->select('id, tree_id, token, title')->from('VideoAlghad')->orderBy('view_count DESC')->limit(3)->fetchArray();
    }

    public function executeHome_shelves() {
        $this->items_1 = DosTable::getInstance()->createQuery()->limit(6)->select('name_en as name, cover_image')->fetchArray();
        if (count($this->items_1) == 6)
            $this->items_2 = DosTable::getInstance()->createQuery()->limit(6)->offset(6)->select('name_en as name, cover_image')->fetchArray();
    }

    public function executeCourse_instructor_doseyeh() {
        $user_faculty_id = $this->getUser()->getGuardUser()->getFacultyId();
        $user_university_id = $this->getUser()->getGuardUser()->getUniversityId();
        $this->courses = CourseTable::getInstance()
                ->createQuery('c')
                ->select('c.id, c.name_en as name')
                ->leftJoin('c.CourseFaculty cf ON c.id=cf.course_id')
                ->leftJoin('c.Faculty f ON cf.faculty_id=f.id')
                ->where('f.university_id = ?', $user_university_id)
                ->fetchArray();
    }

    public function executeSlider() {
              $univ_id = $this->getUser()->getGuardUser()->getUniversityId();
        $q = UniversityNewsTable::getInstance()->createQuery()->where('university_id = ?', $univ_id)->orWhere('university_id IS NULL')->andWhere('expiry_date > ?', date('Y-m-d'));
        $this->univ_news_count = $q->count();
        if ($this->univ_news_count > 0)
            $this->images = $q->fetchArray();
        
    }

}